﻿using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.DTO;
using Pay.TO;
namespace Pay.Token.Controllers {
    [AppAuthorize("Member")]
    public class AdController : GeneralController {

        private IAdvertisementService _iAdvertisementService;
        private IActivityService _iActivityService;
        private IPlayingService _iPlayingService;
        private IPlayingTeamService _iPlayingTeamService;
        private IRedRecordService _iRedRecordService;
        public AdController(IAdvertisementService iAdvertisementService, IActivityService iActivityService, IPlayingService iPlayingService, IPlayingTeamService iPlayingTeamService,IRedRecordService iRedRecordService) {
            _iAdvertisementService = iAdvertisementService;
            _iActivityService = iActivityService;
            _iPlayingService = iPlayingService;
            _iPlayingTeamService = iPlayingTeamService;
            _iRedRecordService = iRedRecordService;
        }

        /// <summary>
        /// 获取广告详情（用于记录广告点击数）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetAd(long id) {
            _iAdvertisementService.AppGetAdvertisement(id);
            return Json(new { msg = "操作成功", isSucceed = true }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 世界杯活动
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult WorldCup() {
            DateTime dt = DateTime.Now;
            var model = _iActivityService.GetActivity(EActivityType.WorldCup);
            ViewBag.img_url = RequestMethod.token_imgurl;
            ViewBag.user_id = UserId;
            var red = _iRedRecordService.GetUser(UserId);
            ViewBag.red = red==null?0:red.present_red_drill;
            _iActivityService.UpdateActivity(EActivityType.WorldCup);
            if (model.begin_time > dt) return View("WorldCupB");
            else if (model.begin_time <= dt && dt <= model.end_time)
                return View(model);
            else return View("WorldCupF");
            
        }
        /// <summary>
        /// 用户竞猜
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Save(user_guess_dto dto) {
            dto.user_id = UserId;
            var result = _iPlayingService.SaveUserGuess(dto);
            if (result) {
                return Json(ResultToJson.Json("竞猜成功", true), JsonRequestBehavior.AllowGet);
            } else {
                return Json(ResultToJson.Json("竞猜失败", false), JsonRequestBehavior.AllowGet);
            }
        }
        [AllowAnonymous]
        public PartialViewResult _Team(long id) {
            var model = _iPlayingTeamService.GetPlayingTeam(id);
            ViewBag.img_url = RequestMethod.token_imgurl;
            return PartialView(model);
        }
        [AllowAnonymous]
        public PartialViewResult _WorldCup(PageQuery query,int type) {
            PlayingQuery playquery = new PlayingQuery();
            playquery.page = query.page;
            playquery.size = query.size;
            playquery.is_end = type > 0 ? true : false;
            var list  = _iPlayingService.GetPlayings(playquery);
            ViewBag.img_url = RequestMethod.token_imgurl;
            ViewBag.user_id = UserId;
            ViewBag.type = type;
            return PartialView(list);
        }
        /// <summary>
        /// 世界杯我的记录
        /// </summary>
        /// <returns></returns>
        public ActionResult WorldCupRecord() {
            return View();
        }
        public PartialViewResult _WorldCupRecord(PageQuery query) {
            UserGuessPlayingHisQuery playQuery = new UserGuessPlayingHisQuery();
            playQuery.page = query.page;
            playQuery.size = query.size;
            playQuery.user_id = UserId;
            
            var list = _iPlayingService.GetUserGuessPlayingHiss(playQuery);
         
            return PartialView(list);
        }

    }
}